from PyQt5.QtWidgets import QApplication, QMainWindow
from BrowserForm import Ui_Dialog
from datetime import date, datetime
# from controller.browser import mains
import os, sys


from model.thread import WorkerThread


# 添加 config 的路径到 sys.path
# sys.path.append(os.path.join(os.path.dirname(__file__), "config"))

class MainWindow(QMainWindow, Ui_Dialog):
    def __init__(self):
        super().__init__()
        self.setupUi(self)  # 调用生成的 setupUi 方法初始化界面
        self.starts.clicked.connect(self.robot_start)  # 绑定按钮

    def robot_start(self):
        # 机器人启动
        number_text1 = self.lineEdit_Resume_Number.text()  # 获取打招呼总数
        print(number_text1)

        # number_text2 = self.lineEdit_Run_Time_2.setText("机器人启动中...")

        time_text1 = self.lineEdit_Run_Time.text()

        time_text2 = self.lineEdit_Run_Time_2.text()

        time_text3 = self.lineEdit_Run_Time_3.text()

        selected_text = self.comboBox_Filter.currentText()
        print("选中的内容是：", selected_text)

        # 获取当前选中的索引（可选）
        selected_index = self.comboBox_Filter.currentIndex()
        print("选中的索引是：", selected_index)
        # main(conditions)
        time_convert1 = None
        time_convert2 = None
        time_convert3 = None
        if time_text3 != '':
            try:
                time_convert1 = self.time_convert(time_text1)
                time_convert2 = self.time_convert(time_text2)
                time_convert3 = self.time_convert(time_text3)

            except Exception as e:
                raise e
        elif time_text2 != '':
            try:
                time_convert1 = self.time_convert(time_text1)
                time_convert2 = self.time_convert(time_text2)

            except Exception as e:
                raise e
        elif time_text1 != '':
            try:
                time_convert1 = self.time_convert(time_text1)

            except Exception as e:
                raise e

        conditions = [number_text1, time_convert1, time_convert2, time_convert3, selected_index]
        self.run_button1(conditions)
        # mains(conditions)
        # # 多线程创建
        # thread_start = WorkerThread(1, conditions)  # 创建task_one的线程
        # print("线程启动")
        # thread_start.start()  # 启动线程

    def time_convert(self, time_text):
        """
        时间转换时间戳
        :return:
        """
        time_list = time_text.split('-')
        specified_time1 = time_list[0]
        specified_time2 = time_list[-1]

        # 获取当前日期
        today = date.today()

        # 将指定时间与当前日期组合
        combined_datetime_str1 = f"{today} {specified_time1}"
        combined_datetime_str2 = f"{today} {specified_time2}"

        # 将字符串转换为 datetime 对象
        combined_datetime1 = datetime.strptime(combined_datetime_str1, "%Y-%m-%d %H:%M:%S")
        combined_datetime2 = datetime.strptime(combined_datetime_str2, "%Y-%m-%d %H:%M:%S")

        # 转换为时间戳（Unix 时间戳）
        timestamp1 = combined_datetime1.timestamp()
        timestamp2 = combined_datetime2.timestamp()

        # print("组合后的日期时间：", combined_datetime)
        print("时间戳：", int(timestamp1), int(timestamp2))
        return [int(timestamp1), int(timestamp2)]

    def run_button1(self, conditions):
        self.thread1 = WorkerThread(1, conditions)  # 创建task_one的线程
        self.thread1.start()  # 启动线程


if __name__ == "__main__":
    import sys

    app = QApplication(sys.argv)
    window = MainWindow()
    window.show()
    sys.exit(app.exec_())
